/**
 * 
 */
package DPSHistory;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.HashMap;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;


/**
 * 
 */
public class ColorLabelRenderer extends DefaultTableCellRenderer {
	
	private HashMap<String, Color> colorMap = new HashMap<String, Color>();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param text
	 */
	public ColorLabelRenderer() {
	}
	
	public Color getColorForName (String name) {
		Color c;
		
		if ((c = colorMap.get(name)) != null) 
			return c;
		
		c = new Color (name.hashCode());
		
		colorMap.put(name, c);
		
		return c;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
	 */
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		String name = table.getColumnName(column);
		
		Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		
		comp.setFont(Font.decode("Arial"));
		
		if (name.equals("PLAYER"))
			comp.setForeground(getColorForName((String) value));				

		else if (name.equals("DPS"))
			comp.setForeground(getScaleColor((Integer) value, 800));
		
		else if  (name.equals("MAX DMG")) // max
			comp.setForeground(getScaleColor((Integer) value, 2000));
				

		else if  (name.equals("AVG DMG")) // avg
			comp.setForeground(getScaleColor((Integer) value, 800));
				

		else if  (name.equals("SUM DMG")) // sum
			comp.setForeground(getScaleColor((Integer) value, 10000));
		
		else if  (name.equals("HITS DMG")) // hits
			comp.setForeground(getScaleColor((Integer) value, 8));				
				

		else if  (name.equals("HPS")) // hps
			comp.setForeground(getScaleColor((Integer) value, 1200));				
				

		else if  (name.equals("MAX HEAL")) // max
			comp.setForeground(getScaleColor((Integer) value, 3000));				
			

		else if  (name.equals("AVG HEAL")) // avg
			comp.setForeground(getScaleColor((Integer) value, 1200));				
				

		else if  (name.equals("SUM HEAL")) //  sum
			comp.setForeground(getScaleColor((Integer) value, 10000));				
				

		else if  (name.equals("HITS HEAL")) // hits
			comp.setForeground(getScaleColor((Integer) value, 8));				
				
		else 
			comp.setForeground(Color.BLACK);
				
	
			
		return comp;
	}

	/**
	 * @param i
	 * @param j 
	 * @return
	 */
	private Color getScaleColor(int val, int max) {

		double sc = (double) val / (double) max;
		
		sc = sc > 1.0 ? 1.0 : (sc < 0 ? 0.0 : sc);
		
		double nsc = 1.0 - sc;
		
		int red = (int) (30 * nsc);
		int green = (int) (255 * sc);
		int blue = 30;
		
		return new Color(red << 16 | green << 8 | blue);
	}

}
